package com.rub.android.ui.activity

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.rub.android.R
import com.rub.android.adapter.ItemsAdapter
import com.rub.android.model.ItemsModel

/**
 * https://www.jianshu.com/p/1e20f301272e
 */
class RecyclerViewAdapterActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //不设置视图，加载的时候页面的值都为null(即都拿不到视图主件)
        setContentView(R.layout.activity_items_recycler_view)

        var recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        var items = ArrayList<ItemsModel>()

        for (i in 1..20) {
            items.add(ItemsModel(title = "我是第${i}条标题", content = "第${i}条内容"))
        }

        //创建布局管理
        var layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView?.layoutManager = layoutManager

        var itemsAdapter = ItemsAdapter(R.layout.activity_items_info, items)

        recyclerView?.adapter = itemsAdapter


        //点击事件
        itemsAdapter.setOnItemClickListener { _, _, position ->
            Toast.makeText(this, "点击了第" + (position + 1) + "条条目", Toast.LENGTH_SHORT).show()
        }

        //长按事件
        itemsAdapter.setOnItemLongClickListener { _, _, position ->
            Toast.makeText(
                this@RecyclerViewAdapterActivity, "长按了第" + (position + 1) + "条条目",
                Toast.LENGTH_SHORT
            ).show()
            false
        }


    }
}